/**
 * 
 */
package info.niwota.profile;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {


	public static final String PROFILE_NAME = "profile.name";
	public static final String PROFILE_NOTE = "profile.note";
	public static final String PROFILE_PHOTO = "profile.photo";
	public static final String PROFILE_LOCATION = "profile.location";
	public static final String PROFILE_TIME = "profile.time";
	
	public static final String PROFILE_GENDER = "profile.gender";
	public static final String PROFILE_DOB = "profile.dob";
	public static final String PROFILE_WELCOME = "profile.welcome";
	public static final String PROFILE_CALL = "profile.call";
	public static final String PROFILE_TEXT = "profile.text";
	public static final String PROFILE_EMAIL = "profile.email";
	public static final String PROFILE_CHAT = "profile.chat";

	public static final String PROFILE_WEBSITE = "profile.website";
	public static final String PROFILE_ADDRESS = "profile.address";
	
	public static final String PROFILE_MODIFIED = "profile.modified";
	
	public static final String PROFILE_PHOTO_MODIFIED = "profile.photo.modified";
	
	private static final String NEVER_MODIFIED = "0";
	
	private static final String PREFS_FAVORITE_PEERS = "favorite_peers";

	
	private Preferences() {
	}

	public static String getProfilePhoto(Context ctx) {
		SharedPreferences sp = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		return sp.getString(PROFILE_PHOTO, null);
	}
	
	public static void setProfilePhoto(Context ctx, String path) {
		final SharedPreferences pref = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor ed = pref.edit();
		ed.putString(PROFILE_PHOTO, path);
		
		long now = System.currentTimeMillis();
		ed.putString(PROFILE_MODIFIED, now+"");
		ed.putString(PROFILE_PHOTO_MODIFIED, now+"");
		ed.commit();
	}

	public static JSONObject getProfileJSONObject(Context ctx, boolean location) throws JSONException {
		JSONObject jo = new JSONObject();
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);

		jo.put(Preferences.PROFILE_NAME, p.getString(Preferences.PROFILE_NAME, ""));
		String loc = null;
		if(location && p.getBoolean(Preferences.PROFILE_LOCATION, false)) {
			loc = Locations.getLocation(ctx);
		}
		jo.put(Preferences.PROFILE_LOCATION, nvp(loc));
		String time = null;
		if(p.getBoolean(Preferences.PROFILE_TIME, false)) {
			time = Dates.getDateTime(ctx);
		}
		jo.put(Preferences.PROFILE_TIME, nvp(time));
		//
		jo.put(Preferences.PROFILE_GENDER, p.getString(Preferences.PROFILE_GENDER, "0"));
		jo.put(Preferences.PROFILE_DOB, p.getString(Preferences.PROFILE_DOB, ""));
		jo.put(Preferences.PROFILE_WELCOME, p.getString(Preferences.PROFILE_WELCOME, ""));
		jo.put(Preferences.PROFILE_CALL, p.getString(Preferences.PROFILE_CALL, ""));
		jo.put(Preferences.PROFILE_TEXT, p.getString(Preferences.PROFILE_TEXT, ""));
		jo.put(Preferences.PROFILE_EMAIL, p.getString(Preferences.PROFILE_EMAIL, ""));
		jo.put(Preferences.PROFILE_CHAT, p.getString(Preferences.PROFILE_CHAT, ""));
		jo.put(Preferences.PROFILE_WEBSITE, p.getString(Preferences.PROFILE_WEBSITE, ""));
		jo.put(Preferences.PROFILE_ADDRESS, p.getString(Preferences.PROFILE_ADDRESS, ""));
		jo.put(Preferences.PROFILE_NOTE, p.getString(Preferences.PROFILE_NOTE, ""));
		
		jo.put(Preferences.PROFILE_MODIFIED, p.getString(Preferences.PROFILE_MODIFIED,  NEVER_MODIFIED ));
		jo.put(Preferences.PROFILE_PHOTO_MODIFIED, p.getString(Preferences.PROFILE_PHOTO_MODIFIED,  NEVER_MODIFIED ));
		
		jo.put(Preferences.PROFILE_PHOTO, !TextUtils.isEmpty(p.getString(Preferences.PROFILE_PHOTO, null)));
		return jo;
	}
	
	private static Object nvp(String s) {
		return (s == null ? "" : s);
	}

	public static long getProfileLastModified(Context ctx) {
		long date = 0;
		try {
			final SharedPreferences sp = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			return Long.parseLong(sp.getString(PROFILE_MODIFIED, NEVER_MODIFIED));
		} catch (Exception e) {
		}
		return date;
	}
	
	public static long getProfilePhotoLastModified(Context ctx) {
		long date = 0;
		try {
			final SharedPreferences sp = PreferenceManager
			.getDefaultSharedPreferences(ctx);
			return Long.parseLong(sp.getString(PROFILE_PHOTO_MODIFIED, NEVER_MODIFIED));
		} catch (Exception e) {
		}
		return date;
	}
	
	public static boolean enableStatusbar(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences sp = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String key = ctx.getString(R.string.settings_statusbar_toggle_key).toString();
			return sp.getBoolean(key, false);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static boolean isPeerStarred(Context ctx, String peer) {
		final SharedPreferences pref = ctx.getSharedPreferences(PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		return isPeerStarred(pref, peer);
	}
	
	public static boolean isPeerStarred(SharedPreferences pref, String peer) {
		return (getPeerStar(pref, peer) != 0);
	}
	
	private static int getPeerStar(SharedPreferences pref, String peer) {
		final int defval = 0;
		try {
			return pref.getInt(peer, defval);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return defval;
	}
	
	public static void starPeer(Context ctx, String peer, boolean star) {
		putPeerStar(ctx, peer, star ? 1 : 0);
	}
	
	/**
	 * star: 1, unstar: 0
	 * 
	 * @param ctx
	 * @param peer
	 * @param star
	 */
	private static void putPeerStar(Context ctx, String peer, int star) {
		final SharedPreferences pref = ctx.getSharedPreferences(PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		try {
			Editor ed = pref.edit();
			ed.putInt(peer, star);
			ed.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static SharedPreferences getFavoritePeers(Context ctx) {
		final SharedPreferences pref = ctx.getSharedPreferences(PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		return pref;
	}

	public static void removeStar(Context ctx, String peer) {
		final SharedPreferences pref = ctx.getSharedPreferences(PREFS_FAVORITE_PEERS, Activity.MODE_PRIVATE);
		pref.edit().remove(peer).commit();
	}
}
